package com.boomshake.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.boomshake.common.Result;
import com.boomshake.dto.StoreCreateDTO;
import com.boomshake.dto.StoreUpdateDTO;
import com.boomshake.service.StoreService;
import com.boomshake.vo.StoreVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 门店管理控制器
 *
 * @author BoomShake
 * @date 2024-12-15
 */
@RestController
@RequestMapping("/admin/store")
@CrossOrigin(origins = {"http://localhost:5173", "http://localhost:5174", "http://127.0.0.1:5173", "http://127.0.0.1:5174"}, 
             methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.PATCH, RequestMethod.DELETE, RequestMethod.OPTIONS},
             allowCredentials = "true", maxAge = 3600)
public class StoreController {

    @Resource
    private StoreService storeService;

    /**
     * 分页查询门店列表
     */
    @GetMapping("/list")
    public Result<IPage<StoreVO>> getStoreList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer status) {
        
        IPage<StoreVO> result = storeService.getStoreList(page, size, name, status);
        return Result.success(result);
    }

    /**
     * 获取门店详情
     */
    @GetMapping("/{id}")
    public Result<StoreVO> getStoreById(@PathVariable Long id) {
        StoreVO store = storeService.getStoreById(id);
        if (store == null) {
            return Result.error("门店不存在");
        }
        return Result.success(store);
    }

    /**
     * 创建门店
     */
    @PostMapping("/create")
    public Result<StoreVO> createStore(@Valid @RequestBody StoreCreateDTO createDTO) {
        try {
            StoreVO store = storeService.createStore(createDTO);
            return Result.success("门店创建成功", store);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新门店信息
     */
    @PutMapping("/update")
    public Result<StoreVO> updateStore(@Valid @RequestBody StoreUpdateDTO updateDTO) {
        try {
            StoreVO store = storeService.updateStore(updateDTO);
            return Result.success("门店更新成功", store);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除门店
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteStore(@PathVariable Long id) {
        try {
            boolean success = storeService.deleteStore(id);
            if (success) {
                return Result.success("门店删除成功", null);
            } else {
                return Result.error("门店删除失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新门店状态
     */
    @PatchMapping("/{id}/status")
    public Result<String> updateStoreStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        
        if (status < 1 || status > 3) {
            return Result.error("状态参数不正确");
        }
        
        try {
            boolean success = storeService.updateStoreStatus(id, status);
            if (success) {
                return Result.success("门店状态更新成功", null);
            } else {
                return Result.error("门店状态更新失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取所有营业中的门店
     */
    @GetMapping("/active")
    public Result<List<StoreVO>> getActiveStores() {
        List<StoreVO> stores = storeService.getActiveStores();
        return Result.success(stores);
    }

    /**
     * 获取门店统计信息
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStoreStatistics() {
        Map<String, Object> statistics = storeService.getStoreStatistics();
        return Result.success(statistics);
    }

    /**
     * 检查门店编码是否存在
     */
    @GetMapping("/check-code")
    public Result<Boolean> checkCodeExists(
            @RequestParam String code,
            @RequestParam(required = false) Long excludeId) {
        
        boolean exists = storeService.checkCodeExists(code, excludeId);
        return Result.success(exists);
    }
} 